import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { TicketService } from '../../../services/ticket.service';
import { RequestedDetailComponent } from '../../invoice-requested/requested-detail/requested-detail.component';

@Component({
  selector: 'app-input-invoice-detail',
  templateUrl: './input-invoice-detail.component.html',
  styleUrls: ['./input-invoice-detail.component.less', '../../../../commom/less/expend-but.less', '../../../../commom/less/box.less']
})
export class InputInvoiceDetailComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  _$expand = false;

  id = null;
  headerInfo: any = {};
  constructor(public service: TicketService, private route: ActivatedRoute) {
    this.id = route.snapshot.params.id;
    this.loadHeadInfo();
  }

  ngOnInit(): void {}

  loadHeadInfo() {
    this.service.request(this.service.$api_get_input_invoice_header, { id: this.id }).subscribe(res => {
      console.log(res);
      if (res) {
        this.headerInfo = res;
      }
    });
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value,
        billTime: {
          start: this.sf?.value.billTime?.[0] || null,
          end: this.sf?.value.billTime?.[1] || null
        },
        feedate: {
          start: this.sf?.value.feedate?.[0] || null,
          end: this.sf?.value.feedate?.[1] || null
        }
      });
    }
    return requestOptions;
  };

  goBack() {
    history.go(-1);
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        feecode: {
          type: 'string',
          title: '费用单',
          ui: {
            placeholder: '请输入'
          }
        },
        billHCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        cno: {
          type: 'string',
          title: '结算客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount()
          },
          default: ''
        },
        feedate: {
          title: '费用日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        billTime: {
          title: '订单日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '序号', render: 'billHCode', width: 40, className: 'text-center' },
      { title: '费用号', index: 'feecode', width: 140 },
      { title: '费用日期', index: 'feedate', width: 150, className: 'text-center' },
      { title: '订单号', index: 'billHCode', width: 100 },
      { title: '订单日期', index: 'billTime', width: 150, className: 'text-center' },
      { title: '结算客户', index: 'cnoName', width: 90 },
      { title: '费用科目', index: 'feesubname', width: 100 },
      { title: '收票金额', render: 'invmoney', width: 120, className: 'text-right' },
      { title: '收票税额', render: 'invtax', width: 100, className: 'text-right' }
    ];
  }
}
